/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.client.param;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.DocumentHelper;

import com.edi.Constant;
import com.edi.db.TransactionWrapper;
import com.edi.db.manager.DBManager;
import com.edi.exceptions.EdiParamException;

public class EdiTranPOClientParam extends EdiClientParam {

	private static final Log log = LogFactory.getLog(EdiTranPOClientParam.class);

	private List<EdiClientParam> ediClientParams;

	//	private String transaction;	

	public EdiTranPOClientParam() {
		this.ediClientParams = new ArrayList<EdiClientParam>();
	}

	public EdiTranPOClientParam(String dsName, String transaction) {

		//初始化返回element cya -2009-7-2
		resultE = DocumentHelper.createElement("result");
		resultE.addElement("state");
		resultE.addElement("dsName");
		resultE.addElement("errorInfo");
		resultE.addElement("table");
		resultE.addElement("object");
		//结束		
		errorMessage = new StringBuffer();
		this.dsName = dsName;
		this.transaction = transaction;
		this.ediClientParams = new ArrayList<EdiClientParam>();
	}

	public void addPoParam(EdiClientParam clientParam) {
		ediClientParams.add(clientParam);
	}

	/**
	 * 执行客户端请求
	 * 
	 * @throws EdiParamException
	 */
	public void excuteClientParam() throws EdiParamException {
		DBManager dbManager = new DBManager(Constant.SERVER_APPNAME);
		List<TransactionWrapper> tranwrappers = new ArrayList<TransactionWrapper>();
		for (EdiClientParam clientParam : ediClientParams) {
			if (clientParam instanceof EdiEPOClientParam) {
				EdiEPOClientParam epoParam = (EdiEPOClientParam) clientParam;
				tranwrappers.add(new TransactionWrapper(epoParam
						.generPreparedEql()));
			} else if (clientParam instanceof EdiTPOClientParam) {
				EdiTPOClientParam tpoParam = (EdiTPOClientParam) clientParam;
				tranwrappers.add(new TransactionWrapper(tpoParam
						.generTableMaps()));
			}
		}
		try {
			dbManager.execTansaction(tranwrappers);
			state = Constant.EXEC_STATE_SUCESS;
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			state = Constant.EXEC_STATE_ERROR;
			errorMessage.append("事务执行失败");
			errorMessage.append(e.getMessage());
			throw new EdiParamException("事务执行失败", e);
		}
	}

}
